//
//  ByUILoginVC.m
//  chequanbao_ios
//
//  Created by mc on 2017/5/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUILoginVC.h"
// view
#import "ByLoginUserOperationView.h"

// vc
#import "ByUIRegistVC.h"
#import "ByUIForgetVC.h"

// api
#import "ByApiUser.h"
@interface ByUILoginVC ()<UITableViewDelegate,UITableViewDataSource>
// view
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) ByLoginUserOperationView *userView;
@property (nonatomic, strong) UIView *footerView;
@end

@implementation ByUILoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    [self addLeftBackBarButtonItem];
    [self initTableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gettext:) name:@"registermodel" object:nil];

}

#pragma mark -- 
- (void)gettext:(NSNotification *)text {
    NSDictionary *dic = text.userInfo;
    UITextField *nameTf = [self.userView viewWithTag:10];
    UITextField *passTf = [self.userView viewWithTag:11];
    nameTf.text = dic[@"username"];
    passTf.text = dic[@"psw"];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"registermodel" object:nil];
}


#pragma mark -- 创建视图
- (void)initTableView{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    _table.backgroundColor = [UIColor clearColor];
    _table.tableFooterView = self.footerView;
}

#pragma mark -- 懒加载
-(ByLoginUserOperationView *)userView{
    if (!_userView) {
         _userView = [[ByLoginUserOperationView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, LineHeight *2 + 120 + 30 + 30) Titles:@[@"手机号",@"密码"] type:ByUserViewLogin ImageAry:nil];
    }
    return _userView;
}

-(UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
        CGFloat width = [@"注册" getSizeOfString:14].width;
        UIButton *registBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, width, 20)];
        [registBtn setTitle:@"注册" forState:UIControlStateNormal];
        [registBtn setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
        registBtn.titleLabel.font = FONT(14);
        [registBtn addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:registBtn];
        
        width = [@"忘记密码？" getSizeOfString:14].width;
        UIButton *forgetBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, width, 20)];
        [forgetBtn setTitle:@"忘记密码？" forState:UIControlStateNormal];
        forgetBtn.right = _footerView.right - 20;
        forgetBtn.titleLabel.font = FONT(14);
        [forgetBtn addTarget:self action:@selector(forgetAction:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:forgetBtn];
        
        
        UIButton *loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, registBtn.bottom + 20, SCREEN_WIDTH - 40, 40)];
        [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        loginBtn.backgroundColor = BY_THEME_COLOR;
        loginBtn.layer.cornerRadius = 4.0;
        [loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:loginBtn];
    }
    return _footerView;
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentier = @"loginCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    [cell addSubview:self.userView];
    return cell;
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return LineHeight * 2 + 120 + 30 + 30;
}

#pragma mark -- 相关点击事件
- (void)loginAction:(UIButton *)btn{
//    [ByCoreToolAlert showOnlyText:@"登录"];
    UITextField *nameTf = [self.userView viewWithTag:10];
    UITextField *passTf = [self.userView viewWithTag:11];

    if (nameTf.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入手机号"];
        return;
    }
    
    if (nameTf.text.length != 11) {
        [ByCoreToolAlert showOnlyText:@"请输入正确的手机号"];
        return;
    }
    
    if (passTf.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入密码"];
        return;
    }
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.username = nameTf.text;
    model.password = passTf.text;
    [ByCoreToolAlert showLoadind:@"登录中..."];
    [ByApiUser login:model success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:@"登录成功"];
        ByModelUser *user = (ByModelUser *)result.data;
        [ByCacheUser setUserInfo:user];
        [self dismissViewControllerAnimated:YES completion:nil];
//        UIWindow *window = [UIApplication sharedApplication].keyWindow;
//        window.rootViewController = [[ByTabBarController alloc] init];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];

}

- (void)forgetAction:(UIButton *)btn{
    ByUIForgetVC *vc = [[ByUIForgetVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

- (void)registAction:(UIButton *)btn{
    ByUIRegistVC *vc = [[ByUIRegistVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}
@end
